package com.huajx.tuopuda.client.udp.handler;

import com.huajx.core.Hboot;
import com.huajx.tuopuda.client.udp.TuopudaDecode;
import com.huajx.tuopuda.client.udp.TuopudaDirect;
import com.huajx.tuopuda.client.udp.TuopudaException;
import com.huajx.tuopuda.client.udp.TuopudaUtil;
import com.huajx.tuopuda.client.udp.config.SchoolConfig;
import com.huajx.util.HexUtil;
import com.huajx.util.date.DateTime;
import com.huajx.util.date.DateUtil;

import java.util.Date;

/**
 * @program: di-parent
 * @description:
 * @author: lisa 叶新华
 * @create: 2019-09-27 11:36
 */
public class SetDatetimeHandler extends AbstractTuopudaHandler{
    
	private static SetDatetimeHandler me;
	public static SetDatetimeHandler me() {
	        if (me == null) {
	            me = new SetDatetimeHandler();
	        }
	        return me;
	 }

	@Override
    public void send(String ip, int port, int sn) {
		SchoolConfig config = Hboot.config(SchoolConfig.class);
		if(config.isInitial()){
			try {
				byte[] setTimebyte = sendAndReceive(ip, port,encode(sn, TuopudaDirect.D_108B));
				System.out.println(HexUtil.bytesToHex(setTimebyte));
				new TuopudaDecode().decode(setTimebyte);
			} catch (TuopudaException e) {
				e.printStackTrace();
			}
		}
    }

    public byte[] encode(int sn, int function) {
    		byte[] bytes2 = encode(sn, function, (bytes) -> {
            Date date = DateUtil.date();
            int year = DateUtil.year(date) - 2000;
            int month = DateUtil.month(date) + 1;
            int day = DateUtil.dayOfMonth(date);
            int hour = DateUtil.hour(date, true);
            int second = DateUtil.second(date);
            int minute = DateUtil.minute(date);

            int week = DateUtil.dayOfWeek(date) - 1;

            if(week == 0) {
                week = 7;
            }

            int checkSum=0;
           /* System.out.println(DateUtil.format(date, "YYMMDDHHmmss"));
            System.out.println(DateTime.now().dayOfWeek());*/
            checkSum += TuopudaUtil.replaceBytes(bytes, HexUtil.hexToBytes(String.format("%2d", year).replace(" ", "0")), 5, 1);
            checkSum += TuopudaUtil.replaceBytes(bytes,  HexUtil.hexToBytes(String.format("%2d", month).replace(" ", "0")), 6, 1);
            checkSum += TuopudaUtil.replaceBytes(bytes,  HexUtil.hexToBytes(String.format("%2d", day).replace(" ", "0")) , 7, 1);
            checkSum += TuopudaUtil.replaceBytes(bytes, HexUtil.hexToBytes(String.format("%2d", week).replace(" ", "0")), 8, 1);
            checkSum += TuopudaUtil.replaceBytes(bytes,HexUtil.hexToBytes(String.format("%2d", hour).replace(" ", "0")) , 9, 1);
            checkSum += TuopudaUtil.replaceBytes(bytes, HexUtil.hexToBytes(String.format("%2d", minute).replace(" ", "0")), 10, 1);
            checkSum += TuopudaUtil.replaceBytes(bytes,  HexUtil.hexToBytes(String.format("%2d", second).replace(" ", "0")), 11, 1);

            return checkSum;
        });
       // System.out.println(HexUtil.bytesToHex(bytes2));
        return bytes2;
    }
    
    public static void main(String[] args) {
    	 String str = String.format("%2d", 1).replace(" ", "0");
    	 System.out.println(str);
	}

}
